;
(function (window, angular) {
	'use strict';
	angular.module("Controller.Corruption.Dashboard", [])
		.controller("DashboardCtrl", ["$scope", "$rootScope", "$state", "$stateParams", function ($scope, $rootScope, $state, $stateParams) {

			const commonMenu = {
				state: '',
				leaf: 0,
				isCollapsed: true
			}

			// 销售管理
			const saleDatas = [
				// 在线监察(8个)
				'app.corruption.priceWarning',
				'app.corruption.priceSchemeWarn',
				'app.corruption.wjServicePartForecast',
				'app.corruption.sellIntentionChangeWarning',
				'app.corruption.unInvoiceWarning',
				'app.corruption.invoiceNotSigned',
				'app.corruption.buyBackWarn',
				'app.corruption.reportReceivableMonthWarn',

				// 督察督办(8个)
				'app.corruption.priceWarningDbd',
				'app.corruption.priceSchemeWarnDbd',
				'app.corruption.wjServicePartForecastDbd',
				'app.corruption.sellIntentionChangeWarningDbd',
				'app.corruption.unInvoiceWarningDbd',
				'app.corruption.invoiceNotSignedDbd',
				'app.corruption.buyBackWarnDbd',
				'app.corruption.reportReceivableMonthWarnDbd'
			]
			const saleMenu = {
				name: '销售管理',
				children: [],
				...commonMenu
			}

			// 采购管理
			const purchaseDatas = [
				// 在线监察(1个)
				'app.corruption.purchaseOrderChangeWarning',

				// 督察督办(1个)
				'app.corruption.purchaseOrderChangeWarningDbd'
			]
			const purchaseMenu = {
				name: '采购管理',
				children: [],
				...commonMenu
			}

			// 仓储物流
			const cangchuWuliuDatas = [
				// 在线监察(4个)
				'app.corruption.purchaseInboundWarn',
				'app.corruption.deliveryNoteNotPostWarn',
				'app.corruption.deliveryNoteWarning',
				'app.corruption.deliveryReceiptWarn',

				// 督察督办(4个)
				'app.corruption.purchaseInboundWarnDbd',
				'app.corruption.deliveryNoteWarningDbd',
				'app.corruption.deliveryNoteNotPostWarnDbd',
				'app.corruption.deliveryReceiptWarnDbd'
			]

			const cangchuWuliuMenu = {
				name: '仓储物流',
				children: [],
				...commonMenu
			}


			const dataGoverMenu = {
				name: '数据治理',
				children: [],
				...commonMenu
			}



			// 通过父菜单名称 查找对应所有子菜单
			function getMenuChildrenByName(val) {
				const corrAllMenus = angular.copy($rootScope.primaryMenuItem.children);
				return corrAllMenus.find(c => c.name == val)?.children ?? []
			}

			// 查找所给菜单的 第一个可用菜单，并把对应父菜单展开。
			function getTargetState(menuData) {
				const childItem = menuData.children[0] || {};
				if (childItem.leaf === 1 && childItem.state !== "") {
					return childItem.state;
				}

				if (childItem.leaf === 0) {
					childItem.isCollapsed = false;
					var childChildItem = childItem.children[0] || {};
					return childChildItem.state;
				}
			}

			// 展开所给state对应的父菜单
			function setParentMenuExpandByState(val) {
				for (let item of $rootScope.subMenu.children) {
					if (item.children.map(pcc => pcc.state).includes(val)) {
						item.isCollapsed = false
						break
					}
				}
			}

			// 重构菜单结构
			function rebuildMenuByName(val, isDefaultMode) {
				const targetChildMenus = getMenuChildrenByName(val)

				const rebuildMenus = []
				
				if (val == '数据治理') {
					dataGoverMenu.children = targetChildMenus
					if (dataGoverMenu.children.length > 0) {
						rebuildMenus.push(dataGoverMenu)
					}
				} else {
					targetChildMenus.forEach(m => {
						if (saleDatas.includes(m.state)) {
							saleMenu.children.push(m)
						}
						if (purchaseDatas.includes(m.state)) {
							purchaseMenu.children.push(m)
						}
						if (cangchuWuliuDatas.includes(m.state)) {
							cangchuWuliuMenu.children.push(m)
						}
					})

					if (saleMenu.children.length > 0) {
						rebuildMenus.push(saleMenu)
					}
					if (purchaseMenu.children.length > 0) {
						rebuildMenus.push(purchaseMenu)
					}
					if (cangchuWuliuMenu.children.length > 0) {
						rebuildMenus.push(cangchuWuliuMenu)
					}
				}

				if (rebuildMenus.length == 0) {
					return
				}

				$rootScope.subMenu.children = [
					$rootScope.subMenu.children[0],
					...rebuildMenus
				]

				isDefaultMode && $state.go(getTargetState({ children: rebuildMenus }))
			}

			$scope.zaixianjiancha = function () {
				rebuildMenuByName('在线监察', true)
			};

			$scope.duchaduban = function () {
				rebuildMenuByName('督察督办', true)
			};

			$scope.shujuzhili = function () {
				rebuildMenuByName('数据治理', true)
			};

			(function () {
				const { fromState } = $stateParams
				if (fromState) {
					for (let pc of $rootScope.primaryMenuItem.children) {
						if (pc.children.map(pcc => pcc.state).includes(fromState)) {
							rebuildMenuByName(pc.name, false)
							break
						}
					}
					setParentMenuExpandByState(fromState)
					$state.go(fromState,{"billNo": $stateParams.billNo})
				}
			})();
		}])
		;
})(window, window.angular);